package com.evaluation_system.service.impl;

import com.evaluation_system.constant.MessageConstant;
import com.evaluation_system.exception.AccountNotFoundException;
import com.evaluation_system.exception.PasswordErrorException;
import com.evaluation_system.mapper.UserMapper;
import com.evaluation_system.pojo.DTO.ResetPasswordDTO;
import com.evaluation_system.pojo.entity.LoginInfo;
import com.evaluation_system.service.ResetPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RestPasswordServiceImpl implements ResetPasswordService {



    @Autowired
    UserMapper userMapper;

    @Override
    public boolean uploaderresetpassword(ResetPasswordDTO resetPasswordDTO) {
        String username = resetPasswordDTO.getUsername();
        String oldPassword = resetPasswordDTO.getPassword();        // 原密码
        String newPassword = resetPasswordDTO.getNewPassword();     // 新密码

        LoginInfo loginInfo = userMapper.uploaderbyusername(username);

        // 1. 用户不存在
        if (loginInfo == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 2. 密码错误（如用加密方式存储，请使用 BCryptPasswordEncoder 等）
        if (!oldPassword.equals(loginInfo.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        loginInfo.setPassword(newPassword);


        // 3. 更新密码
        int rows = userMapper.updateUploaderPassword(loginInfo);
        return rows > 0;
    }

    @Override
    public boolean checkerresetpassword(ResetPasswordDTO resetPasswordDTO) {
        String username = resetPasswordDTO.getUsername();
        String oldPassword = resetPasswordDTO.getPassword();        // 原密码
        String newPassword = resetPasswordDTO.getNewPassword();     // 新密码

        LoginInfo loginInfo = userMapper.checkerbyusername(username);

        // 1. 用户不存在
        if (loginInfo == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 2. 密码错误
        if (!oldPassword.equals(loginInfo.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 3. 设置新密码
        loginInfo.setPassword(newPassword);

        // 4. 更新密码
        int rows = userMapper.updateCheckerPassword(loginInfo);
        return rows > 0;
    }

    @Override
    public boolean evaluatorresetpassword(ResetPasswordDTO resetPasswordDTO) {
        String username = resetPasswordDTO.getUsername();
        String oldPassword = resetPasswordDTO.getPassword();        // 原密码
        String newPassword = resetPasswordDTO.getNewPassword();     // 新密码

        LoginInfo loginInfo = userMapper.evaluatorbyusername(username);

        // 1. 用户不存在
        if (loginInfo == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 2. 密码错误
        if (!oldPassword.equals(loginInfo.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 3. 设置新密码
        loginInfo.setPassword(newPassword);

        // 4. 更新密码
        int rows = userMapper.updateEvaluatorPassword(loginInfo);
        return rows > 0;
    }


//    @Override
//    public boolean uploaderfind(String username) {
//        return userMapper.uploaderfind(username);
//    }
}
